const path=require('path');
const webpack=require('webpack');
const HtmlwebpackPlugin=require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const glob = require('glob');

let arr=glob.sync("./src/!(common)/*.js")

let entryObj={}
let Htmls=[];
let chunks=[];

for (let item of arr){
    let name=item.substring(item.lastIndexOf('/')+1,item.lastIndexOf('.'));
    entryObj[name]=['./dev/devclient.js',item];

    Htmls.push(new HtmlwebpackPlugin({
        filename:name+'.html',
        template:item.substring(0,item.lastIndexOf('.'))+'.html',
        inject:true,
        chunks:['vendor',name]
    }))

    chunks.push(name)
}

// console.log(entryObj,Htmls)


module.exports={
    mode:'development',
    entry:entryObj,
    output:{
        filename:'[name].[hash].js',
        path:path.resolve(__dirname,'../dist'),
        chunkFilename:'vendor.[hash].js'
    },
    module:{
        rules:[
            {
                test:/\.js$/,
                use:['babel-loader'],
                include:[path.resolve(__dirname,'../index.js'),path.resolve(__dirname,'../src')]
            },
            {
                test:/\.css$/,
                use:ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            }
        ]
    },
    
    optimization:{
        splitChunks:{
            chunks:'all',
            minChunks:1,
            minSize:0,
            name:"vendor"
        }
    },
    plugins:[
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoEmitOnErrorsPlugin(),
        new ExtractTextPlugin({
            filename:'[name].[hash].css'
        }),
        ...Htmls
    ]
}